package net.ba.models
{
	import com.google.maps.LatLng;
	
	import de.polygonal.ds.HashMap;
	
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;
	
	import net.ba.utils.CellIdUtils;
	
	public class Species
	{
		public var id:Number;
		public var guid:String;
		public var gbifId:Number;
		[Bindable]
		public var scientificName:String;
		public var group:Number;
		public var colorPattern:ColorPattern;
		public var cellOccs:ArrayCollection = new ArrayCollection(); 
		public var cellArray:CellArray = new CellArray();
		public var sources:Object = new Object();
		public var totalOcc:Number=0;
		public var totalCells:Number =0;
		
		
		/**
		 * We use this function to add cellOcs in order to update
		 * stats
		 */
		public function addCellOcc(cellOcc:CellOcc):void {
			
			
			//Check if the cellId is already in the model
			// TODO !!!! This can highly be improved!!!
			var found:Boolean=false;
			for each (var c:CellOcc in cellOccs) {
				if (c.cellId == cellOcc.cellId) {
					totalOcc = totalOcc + cellOcc.count;
					c.count = c.count + cellOcc.count;
					//Sum one to the source
					sources[c.source] += c.count;
					found=true;
					break;
				} 
				
			}
			if (!found) {
				cellOccs.addItem(cellOcc);
				totalCells++;
				totalOcc = totalOcc + cellOcc.count;
				if (sources[cellOcc.source] == null) {
					sources[cellOcc.source] = cellOcc.count;
				} else {
					sources[cellOcc.source] += cellOcc.count;				
				}
			}  	
		
		}
		
		/**
		 * To insert cells by LatLng
		 */
		 public function addCellOccByLatLng(latLng:LatLng, count:Number=1,source:String=null):void {
		 	var cellId:Number = CellIdUtils.toCellId(latLng.lat(),latLng.lng());
	  		var cellOcc:CellOcc = new CellOcc(cellId,count,null,source);
	  		addCellOcc(cellOcc);
		 }
		 

	}
}